package org.hackystat.devcathlon.ui.page.teams.manage;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.PackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.image.resource.BufferedDynamicImageResource;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.engine.team.TeamManager;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.ui.page.DevcathlonBasePage;
import org.hackystat.devcathlon.ui.page.teams.profile.TeamsProfile;

/**
 * Implements the Teams Manage page, which provides users to create, edit, and delete teams. 
 * the current user. 
 * @author John Ly
 */
public class TeamsManagePage extends DevcathlonBasePage {
  /** Support serialization. */
  private static final long serialVersionUID = 1L;  
  
  /**
   * Creates the TeamsManage page.  
   * This page and its subpages contain information about this specific user.  
   */
  public TeamsManagePage () {
    final String teamLogoId = "TeamLogo";
    // Set the body class attribute to section-1 so that the menubar is correct.
    add(new WebMarkupContainer("body") { 
      private static final long serialVersionUID = 1L;
      @Override
      public boolean isTransparentResolver() { 
        return true; 
        } 
      @Override
      protected void onComponentTag(ComponentTag tag) { 
        tag.put("class","section-2"); 
        }
    }); 
    
    // Make the tab corresponding to this page bold.
    get("TeamsManageLink").add(new SimpleAttributeModifier("class", "selected"));
    
    // Retrieves all teams associated with the current user.
    String email = DevcathlonSession.get().getEmail();
    List<Team> teams = TeamManager.getInstance().getOwnedTeams(email);      
    
    // The teams you own and thus edit, delete
    ListView ownedList = new ListView("TeamsList", teams) {      
      private static final long serialVersionUID = 1L;

      @Override
      protected void populateItem(ListItem item) {
        final Team team = (Team) item.getModelObject();
        
        try {
          BufferedDynamicImageResource resource = new BufferedDynamicImageResource();     
          BufferedImage image;      
          image = ImageIO.read(new File(team.getTeamLogoPath()));
          resource.setImage(image);
          item.add(new Image(teamLogoId, resource)); 
        }
        catch (IOException e) {
          item.add(new Image(teamLogoId));
        }
        
        Link teamProfileLink = new Link("TeamProfileLink") {
          private static final long serialVersionUID = 1L;
          
          @Override
          public void onClick() {
            DevcathlonSession.get().getTeamsSession().setSelectedTeam(team.getName());
            setResponsePage(new TeamsProfile());
          }          
        };
        teamProfileLink.add(new Label("TeamNameLabel", team.getName()));
        
        item.add(teamProfileLink);
        item.add(new Label("TeamProjectLabel", team.getProjectName()));
        item.add(new Label("TeamOwnerLabel", 
            team.getOwner().getFirstName() + " " + team.getOwner().getLastName()));
        item.add(new Link("TeamEditLink") {
          private static final long serialVersionUID = 1L;
                    
          @Override
          public void onClick() {
            
            DevcathlonSession.get().getTeamsSession().setSelectedTeam(team.getName());
            setResponsePage(new TeamsManageEditPage());            
          }          
        });
        item.add(new Link("TeamDeleteLink") {
          private static final long serialVersionUID = 1L;          
          @Override
          public void onClick() {
            DevcathlonSession.get().getTeamsSession().setSelectedTeam(team.getName());
            setResponsePage(new TeamsManageDeletePage());            
          }          
        });
      }
    };       
    add(ownedList);
    
    // -----------------------------------------------------
    
    // The teams you can resign from    
    List<Team> unownedTeams = TeamManager.getInstance().getUnownedTeams(email);
    ListView memberships = new ListView("MembershipList", unownedTeams) {      
      private static final long serialVersionUID = 1L;

      @Override
      protected void populateItem(ListItem item) {
        final Team team = (Team) item.getModelObject();
        
        try {
          BufferedDynamicImageResource resource = new BufferedDynamicImageResource();     
          BufferedImage image;      
          image = ImageIO.read(new File(team.getTeamLogoPath()));
          resource.setImage(image);
          item.add(new Image(teamLogoId, resource)); 
        }
        catch (IOException e) {
          item.add(new Image(teamLogoId));
        }
        
        Link teamProfileLink = new Link("TeamProfileLink") {
          private static final long serialVersionUID = 1L;
          
          @Override
          public void onClick() {
            DevcathlonSession.get().getTeamsSession().setSelectedTeam(team.getName());
            setResponsePage(new TeamsProfile());
          }          
        };
        teamProfileLink.add(new Label("TeamNameLabel", team.getName()));
        
        item.add(teamProfileLink);
        item.add(new Label("TeamProjectLabel", team.getProjectName()));
        item.add(new Label("TeamOwnerLabel", 
            team.getOwner().getFirstName() + " " + team.getOwner().getLastName()));
        
        Link resignLink = new Link("TeamResignLink") {
          private static final long serialVersionUID = 1L;          
          @Override
          public void onClick() {
            DevcathlonSession.get().getTeamsSession().setSelectedTeam(team.getName());
            setResponsePage(new TeamsManageResignPage());            
          }          
        };
        resignLink.add(new Image("resignIcon", 
            PackageResource.get(TeamsManagePage.class, "Resign.png")));
        
        item.add(resignLink);
      }
    };       
    add(memberships);      
    
    // Creates and sets a message if no teams are displayed.
    String status = "";
    if (teams.isEmpty() && unownedTeams.isEmpty()) {
      status = "You are not apart of any teams";
    }
    add(new Label("StatusLabel", status));      

  }
  
  

}
